(function(KKBus){

    function NewsController() {
        var self = this;
        
        self.news = ko.observableArray();

        self.getNews = function(page) {
            // można tutaj sprawdzać czy danych już nie ma w cache
            // lub czyścimi wszystko i nowy request do serwa
            self.news.removeAll();
            
            app.request++;
            
            var url = 'rest/news';
            if (page > 0) {
                // dodaj page do url
            }
            
            KKBus.Ajax.doGet(url, function(data, textStatus, jqXHR) {
                var jsData = JSON.parse(data);
                var i = jsData.length;
                
                while(i--) {
                    
                    self.news.push(new KKBus.NewsModel(jsData[i].id, jsData[i].title, jsData[i].content, KKBus.Format.timestampToDate(jsData[i].date)));    
                }
                
                app.request--;
                
                if (app.request == 0) {
                    if (app.settings.DEBUG == true) { console.log("Load template from: " + NewsController.name); }
                    setTimeout(function() { app.loadTemplate(); }, 1000);
                }
            });
        };
        
         self.deleteNews = function(id, index) {
         
         self.news.remove(self.news()[index]);    
             
         KKBus.Ajax.doDelete('rest/news/'+id, function(result, textStatus, jqXHR) {
    });   
    };    
       
       self.validation = ko.observable(false);
       self.errorMessage = ko.observable("Blad");
       self.success = ko.observable(false); 
            
       self.addNews = function() {
        
            var news = JSON.stringify({
                    title: $('#formTitle').val(),
                    content: $('#formContent').val(),
                    date: $('#formDate').val(),                  
          
                });
             
       if(KKBus.Validation.length($('#formTitle').val())
          && KKBus.Validation.length($('#formContent').val())
          && KKBus.Validation.length($('#formDate').val())){   
             
                  if(KKBus.Validation.data($('#formDate').val())){
               
               
               
                            KKBus.Ajax.doPost('rest/news/', news, function(result, textStatus, jqXHR) {
                                if (result == "ADDED") {
                                 
                                       
                                        self.success(true);
                                        $('#formTitle').val(' ');
                                        $('#formContent').val(' ');
                                        $('#formDate').val(' ');
                                        setTimeout(function() {
                                                            
                                                        
                                            
                                        self.success(false);
                                          }, 3000);
                                        
                                        
                                      var temp = JSON.parse(news);

                                          var NewsAdd = new KKBus.NewsModel(temp.id,temp.title,temp.content,"2013-06-15");

                                          self.news.push(NewsAdd);                                }
                 });
                 
                }else{
                    
                    self.validation(true);
                    self.errorMessage("Błędny format daty!");
                    setTimeout(function() { self.validation(false); }, 3000);
                    
                };
                 
          }else{
              
              self.validation(true);
              self.errorMessage("Puste pola!");
              setTimeout(function() { self.validation(false); }, 3000);
              
          };
         
    };
    
    
    
    }

    KKBus.NewsController = NewsController;
    
}(window.KKBus));